﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SelectedRowsContainer
{
    public partial class frmSelectRowList : Form
    {

        public object DataSource { get; set; }
        public DataGridView GridView { get; set; }
        public List<DataGridViewColumn> ShowColumnName { get; set; }

        public DataGridViewColumn KeyColumn { get; set; }

        public List<object> RemovedKeys { get; set; }

        public frmSelectRowList()
        {
            InitializeComponent();
        }

        private void frmSelectRowList_Load(object sender, EventArgs e)
        {
            if (ShowColumnName == null || ShowColumnName.Count == 0)
            {
                return;
            }

            InitColumn();

            if (DataSource is List<KeyValuePair<object, object>> pairs)
            {
                dataGridView1.DataSource = pairs.Select(m => m.Value).ToList();
            }
            else
            {
                dataGridView1.DataSource = DataSource;
            }

            SetTitleText();
        }

        private void SetTitleText()
        {
            Text = $"已选择({dataGridView1.Rows.Count})行";
        }

        private void InitColumn()
        {
            if (KeyColumn == null)
            {
                throw new ArgumentNullException("KeyColumn can not be null");
            }

            var keyColumnAdded = false;
            foreach (var column in ShowColumnName)
            {
                var col = new DataGridViewTextBoxColumn
                {
                    DataPropertyName = column.DataPropertyName,
                    ReadOnly = true,
                    Width = column.Width,
                    HeaderText = column.HeaderText
                };

                dataGridView1.Columns.Add(col);
                if (column.DataPropertyName == KeyColumn.DataPropertyName)
                {
                    keyColumnAdded = true;
                }
            }

            if (!keyColumnAdded)
            {
                var col = new DataGridViewTextBoxColumn
                {
                    DataPropertyName = KeyColumn.DataPropertyName,
                    Width = KeyColumn.Width,
                    HeaderText = KeyColumn.HeaderText,
                    Visible = false
                };

                dataGridView1.Columns.Add(col);
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1 || e.ColumnIndex != colRemove.Index)
            {
                return;
            }

            var key = dataGridView1.Rows[e.RowIndex].Cells[KeyColumn.Index].Value;
            if (key is null || key is DBNull)
            {
                return;
            }
            if(RemovedKeys == null) RemovedKeys = new List<object>();
            RemovedKeys.Add(key);
            dataGridView1.DataSource = null;

            if (DataSource is DataTable dt)
            {
                dt.Rows.RemoveAt(e.RowIndex);
                dataGridView1.DataSource = DataSource;
            }
            else if (DataSource is List<KeyValuePair<object, object>> pairs)
            {
                var index = -1;
                for (int i = 0; i < pairs.Count; i++)
                {
                    if (pairs[i].Key.Equals(key))
                    {
                        index = i;
                        break;
                    }
                }
                if (index <= -1) return;
                pairs.RemoveAt(index);
                dataGridView1.DataSource = pairs.Select(m => m.Value).ToList();
            }

            SetTitleText();
        }
    }
}
